package Stack;
//使用链表模拟栈
public class LinkedListStatck {
    public static void main(String[] args) {
        LStack listStatck = new LStack();
        for (int i = 0; i < 10 ; i++) {
            listStatck.push(new Node(i));
        }
        listStatck.list();
    }
}
class LStack{
    private Node top = new Node(-1);//top指向头顶元素的前一个元素
    public boolean isEmpty(){
        return top.getNext() == null;
    }
    public void push(Node node){
        if (top.getNext() == null){
            //此时栈空

        }else {
            node.setNext(top.getNext());
        }
        top.setNext(node);
    }
    public Node pop(){
        if (isEmpty()){
            System.out.println("栈是空的");
            return null;
        }
        Node node = top.getNext();
        top.setNext(node.getNext());
        return node;

    }
    public void list(){
        while (true){
            if(top.getNext() == null){
                break;
            }
            System.out.printf("%d\n",top.getNext().getValue());
            top = top.getNext();
        }
    }
}
class Node{
    private int value;
    private Node next;

    public Node(int value) {
        this.value = value;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public Node getNext() {
        return next;
    }

    public int getValue() {
        return value;
    }
}